package com.material.supplier.service.impl;

import java.util.List;

import com.material.common.core.domain.entity.SysRole;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.exception.ServiceException;
import com.material.common.utils.DateUtils;
import com.material.common.utils.PinyinUtils;
import com.material.common.utils.SecurityUtils;
import com.material.factory.domain.FactoryInfo;
import com.material.factory.mapper.FactoryInfoMapper;
import com.material.supplier.vo.SupplierInfoVo;
import com.material.system.mapper.SysRoleMapper;
import com.material.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.material.supplier.mapper.SupplierInfoMapper;
import com.material.supplier.domain.SupplierInfo;
import com.material.supplier.service.ISupplierInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class SupplierInfoServiceImpl implements ISupplierInfoService 
{
    @Autowired
    private SupplierInfoMapper supplierInfoMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private FactoryInfoMapper factoryInfoMapper;

    /**
     * 查询供应商信息
     * 
     * @param id 供应商信息主键
     * @return 供应商信息
     */
    @Override
    public SupplierInfo selectSupplierInfoById(Long id)
    {
        return supplierInfoMapper.selectSupplierInfoById(id);
    }

    /**
     * 查询供应商信息列表
     * 
     * @param supplierInfo 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<SupplierInfo> selectSupplierInfoList(SupplierInfo supplierInfo)
    {
        return supplierInfoMapper.selectSupplierInfoList(supplierInfo);
    }

    @Override
    public List<SupplierInfoVo> selectSupplierList(SupplierInfo supplierInfo) {
        return supplierInfoMapper.selectSupplierList(supplierInfo);
    }

    /**
     * 新增供应商信息
     * 
     * @param supplierInfo 供应商信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSupplierInfo(SupplierInfo supplierInfo)
    {
        FactoryInfo factoryInfo = factoryInfoMapper.selectFactoryInfoById(supplierInfo.getFactoryId());
        String firstLetters = PinyinUtils.getFirstLetters(factoryInfo.getName());
        String loginName = firstLetters + supplierInfo.getMobile();
        supplierInfo.setLoginName(loginName);
        supplierInfo.setCreateTime(DateUtils.getNowDate());
        SupplierInfo exist = supplierInfoMapper.selectSupplierInfoByLoginName(loginName);
        if(exist != null){
            throw new ServiceException("供应商已存在");
        }
        supplierInfoMapper.insertSupplierInfo(supplierInfo);
        SysRole supplier = sysRoleMapper.checkRoleKeyUnique("supplier");
        SysUser user = new SysUser();
        Long[] roles = new Long[]{supplier.getRoleId()};
        user.setRoleIds(roles);
        user.setUserType(SysUser.USER_TYPE_3);
        user.setBizId(supplierInfo.getId());
        user.setUserName(loginName);
        user.setPassword(SecurityUtils.encryptPassword("123456"));
        user.setAvatar("https://smart-fuel.oss-cn-hangzhou.aliyuncs.com/2025/07/15/20250715110146.png");
        user.setNickName(supplierInfo.getRealName());

        return sysUserService.insertUser(user);
    }

    /**
     * 修改供应商信息
     * 
     * @param supplierInfo 供应商信息
     * @return 结果
     */
    @Override
    public int updateSupplierInfo(SupplierInfo supplierInfo)
    {
        return supplierInfoMapper.updateSupplierInfo(supplierInfo);
    }

    /**
     * 批量删除供应商信息
     * 
     * @param ids 需要删除的供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteSupplierInfoByIds(Long[] ids)
    {
        return supplierInfoMapper.deleteSupplierInfoByIds(ids);
    }

    /**
     * 删除供应商信息信息
     * 
     * @param id 供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteSupplierInfoById(Long id)
    {
        return supplierInfoMapper.deleteSupplierInfoById(id);
    }
}
